package com.acme.bankapp.domain.account;

import com.acme.bankapp.exceptions.NotEnoughFundsException;

import java.io.Serializable;

public abstract class AbstractAccount implements Account,Serializable {

    private double balance;

    public AbstractAccount(final double amount) throws IllegalArgumentException {
        if (amount < 0) {
            throw new IllegalArgumentException("Not legal parameter for amount");
        }
        this.balance = amount;
    }

    public double getBalance() {
        return this.balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public void deposit(final double amount) {
        if (amount > 0) {
            this.balance += amount;
        }
    }

    public void withdraw(final double amount) throws NotEnoughFundsException{
        if (this.maximumAmountWithDraw() < amount)
            throw new NotEnoughFundsException(this);
        this.setBalance(this.getBalance() - amount);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof AbstractAccount)) return false;

        AbstractAccount that = (AbstractAccount) o;

        return Double.compare(that.balance, balance) == 0;
    }

    @Override
    public int hashCode() {
        long temp = Double.doubleToLongBits(balance);
        return (int) (temp ^ (temp >>> 32));
    }

    @Override
    public String toString() {
        return "balance=" + decimalValue()+";";
    }

    @Override
    public long decimalValue() {
        return Math.round(this.getBalance());
    }
}
